/***************************************************************************
 * 文件名:EtVersionManager.java<br>
 * 版本: <br>
 * 描述: <br>术语版本
 * 版权所有: <br>
 * //////////////////////////////////////////////////////// <br>
 * 创建者: robo<br>
 * 创建日期: 2010-11-10 <br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 **************************************************************************/
package com.sunwayhorizo.health.ets.base.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import org.jboss.seam.Component;
import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.business.dto.ResultsDTO;
import com.sunwayhorizo.health.common.ListUtil;
import com.sunwayhorizo.health.ets.base.common.BaseDao;
import com.sunwayhorizo.health.ets.base.persistent.EtCodingSchemes;
import com.sunwayhorizo.health.ets.base.persistent.EtVersions;
import com.sunwayhorizo.health.ets.dto.EtVersionsDTO;
import com.sunwayhorizo.health.ets.dto.TreeDTO;

@Name("EtVersionsManager")
@Stateless
@SuppressWarnings("unchecked")
public class EtVersionsManager extends BaseDao implements
		EtVersionsManagerLocal {

	public List<EtVersions> findEtVersions() throws Exception {
		return entityManager2.createQuery("select o from EtVersions o ")
				.getResultList();
	}

	public ResultsDTO checkUnique(EtVersionsDTO etDto) throws Exception
	{
		dto=findEtVersions(etDto.getCodingschemeId(),etDto.getVersionName());
		List<EtVersionsDTO> list=(List<EtVersionsDTO>)dto.getObject();
		dto.setObject(list.size());
		dto.setResultFlag(true);
		dto.setResultDesc("唯一性验证成功");
		return dto;
	}
	public ResultsDTO findEtVersions(String codingSchemesId, String versionName) throws Exception {
		ResultsDTO model = new ResultsDTO(0, 0);
		String jpql = "select o from EtVersions o where 1=1 ";
		Query query = getJpql(codingSchemesId, versionName, jpql);
		List<EtVersions> list = query.getResultList();
		List<EtVersionsDTO> returnList = new ArrayList<EtVersionsDTO>();
		for (EtVersions et : list) {
			EtVersionsDTO dto = new EtVersionsDTO();
			dto.setCodingschemeId(et.getEtCodingSchemes().getCodingschemeId());
			dto.setCreatedBy(et.getCreatedBy());
			dto.setCreationDate(et.getCreationDate());
			dto.setDefaultFlag(et.getDefaultFlag());
			dto.setHistoryTypeCode(et.getHistoryTypeCode());
			dto.setLastUpdateDate(et.getLastUpdateDate());
			dto.setLastUpdatedBy(et.getLastUpdatedBy());
			dto.setLastUpdateLogin(et.getLastUpdateLogin());
			dto.setLoadDate(et.getLoadDate());
			dto.setRetiredDate(et.getRetiredDate());
			dto.setStatusCode(et.getStatusCode());
			dto.setVersionDesc(et.getVersionDesc());
			dto.setVersionId(et.getVersionId());
			dto.setVersionName(et.getVersionName());
			returnList.add(dto);
		}
		model.setObject(returnList);
		model.setResultFlag(true);
		model.setResultDesc("查詢成功");
		return model;
	}

	private Query getJpql(String codingSchemesId, String versionName,
			String jpql) {
		if (codingSchemesId != null && !codingSchemesId.equals("")) {
			jpql += " and o.etCodingSchemes.codingschemeId =:codingSchemesId ";
		}
		if (versionName != null && !versionName.equals("")) {
			jpql += " and o.versionName ='" + versionName + "' ";
		}
		jpql +=" order by o.versionId asc ";
		Query query = entityManager2.createQuery(jpql);

		if (codingSchemesId != null && !codingSchemesId.equals("")) {
			query.setParameter("codingSchemesId", codingSchemesId);
		}
		return query;
	}

	public int findEtVersionsCount(String codingSchemesId, String versionName)
			throws Exception {
		String jpql = "select count(o.versionId) from EtVersions o where 1=1 ";
		Query query = getJpql(codingSchemesId, versionName, jpql);
		List<Long> list = query.getResultList();
		if (list != null && list.size() > 0) {
			return list.get(0).intValue();
		}
		return 0;
	}

	@Override
	public ResultsDTO newEtVersions(EtVersionsDTO dto) throws Exception {
		ResultsDTO resultsDTO = new ResultsDTO();

		EtVersions ev = new EtVersions();
		ev.setVersionName(dto.getVersionName());
		ev.setVersionDesc(dto.getVersionDesc());
		ev.setCreatedBy(dto.getCreatedBy());
		ev.setCreationDate(dto.getCreationDate());
		ev.setDefaultFlag(dto.getDefaultFlag());
		EtCodingSchemes ecs = new EtCodingSchemes();
		ecs.setCodingschemeId(dto.getCodingschemeId());
		ev.setEtCodingSchemes(ecs);
		ev.setHistoryTypeCode(dto.getHistoryTypeCode());
		ev.setLastUpdateDate(dto.getLastUpdateDate());
		ev.setLastUpdatedBy(dto.getLastUpdatedBy());
		ev.setLastUpdateLogin(dto.getLastUpdateLogin());
		ev.setLoadDate(dto.getLoadDate());
		ev.setRetiredDate(dto.getRetiredDate());
		ev.setStatusCode(dto.getStatusCode());
		entityManager2.persist(ev);
		resultsDTO.setObject(ev.getVersionId());

		resultsDTO.setResultDesc("保存成功");
		resultsDTO.setResultFlag(true);
		return resultsDTO;
	}

	public ResultsDTO newEtVersions(List<EtVersionsDTO> list) throws Exception {
		ResultsDTO resultsDTO = new ResultsDTO();
		if (list != null && list.size() > 0) {
			for (EtVersionsDTO dto : list) {
				EtVersions ev = new EtVersions();
				ev.setVersionName(dto.getVersionName());
				ev.setVersionDesc(dto.getVersionDesc());
				ev.setCreatedBy(dto.getCreatedBy());
				ev.setCreationDate(dto.getCreationDate());
				ev.setDefaultFlag(dto.getDefaultFlag());
				EtCodingSchemes ecs = new EtCodingSchemes();
				ecs.setCodingschemeId(dto.getCodingschemeId());
				ev.setEtCodingSchemes(ecs);
				ev.setHistoryTypeCode(dto.getHistoryTypeCode());
				ev.setLastUpdateDate(dto.getLastUpdateDate());
				ev.setLastUpdatedBy(dto.getLastUpdatedBy());
				ev.setLastUpdateLogin(dto.getLastUpdateLogin());
				ev.setLoadDate(dto.getLoadDate());
				ev.setRetiredDate(dto.getRetiredDate());
				ev.setStatusCode(dto.getStatusCode());
				entityManager2.persist(ev);
				resultsDTO.setObject(ev.getVersionId());
				entityManager2.flush();
			}
		}
		resultsDTO.setResultDesc("保存成功");
		resultsDTO.setResultFlag(true);
		return resultsDTO;
	}

	public ResultsDTO updateEtVersions(EtVersionsDTO dto)
			throws Exception {
		ResultsDTO resultsDTO = new ResultsDTO();
		if (dto != null) {
			EtVersions ev = entityManager2.find(EtVersions.class,
					dto.getVersionId());
			ev.setVersionName(dto.getVersionName());
			ev.setVersionDesc(dto.getVersionDesc());
			ev.setCreatedBy(dto.getCreatedBy());
			ev.setCreatedByName(dto.getCreatedByName());
			ev.setCreationDate(dto.getCreationDate());
			ev.setDefaultFlag(dto.getDefaultFlag());
			EtCodingSchemes ecs = new EtCodingSchemes();
			ecs.setCodingschemeId(dto.getCodingschemeId());
			ev.setEtCodingSchemes(ecs);
			ev.setHistoryTypeCode(dto.getHistoryTypeCode());
			ev.setLastUpdateDate(dto.getLastUpdateDate());
			ev.setLastUpdatedBy(dto.getLastUpdatedBy());
			ev.setLastUpdatedByName(dto.getLastUpdatedByName());
			ev.setLastUpdateLogin(dto.getLastUpdateLogin());
			ev.setLoadDate(dto.getLoadDate());
			ev.setRetiredDate(dto.getRetiredDate());
			ev.setStatusCode(dto.getStatusCode());
			entityManager2.merge(ev);
		}
		resultsDTO.setResultDesc("修改成功");
		resultsDTO.setResultFlag(true);
		return resultsDTO;
	}

	public ResultsDTO delEtVersions(List<String> list) throws Exception {
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				String id = (String) list.get(i);
				EtConceptsManagerLocal  evmLocal = (EtConceptsManagerLocal)Component.getInstance("EtConceptsManager");
				int etConceptsCount=evmLocal.findEtConceptsCount(null, null, id);
				if(etConceptsCount==0)
				{
					EtVersions ev = entityManager2.find(EtVersions.class, id);
					entityManager2.remove(ev);
					dto.setResultDesc("刪除成功");
					dto.setResultFlag(true);
				}else
				{
					dto.setResultDesc("删除失败：请先删除与该术语版本关联的术语");
					dto.setResultFlag(false);
				}
			}
		}
		return dto;
	}

	public ResultsDTO findCodingSchemesVersionsTree() throws Exception {
		List<EtVersions> list = entityManager2.createNamedQuery(
				"EtVersions.findAll").getResultList();
		List<Object> returnList = new ArrayList<Object>();
		List<Object> newList = new ArrayList<Object>();
		List<Object> resultList = new ArrayList<Object>();

		// 多做了一层，可以少减一层操作
		if (list != null && !list.isEmpty()) {
			for (EtVersions ev : list) {
				String codingSchemesId = ev.getEtCodingSchemes()
						.getCodingschemeId();
				String codingSchemesName = ev.getEtCodingSchemes()
						.getCodingschemeChName();
				String versionId = ev.getVersionId();
				String versionName = ev.getVersionName();
				Object[] o = { codingSchemesId, codingSchemesName, versionId,
						versionName };
				returnList.add(o);
			}

			for (int i = 0; i < returnList.size(); i++) {
				Object[] o = (Object[]) returnList.get(i);
				String codingSchemesId = (String) o[0];
				String codingSchemesName = (String) o[1];

				TreeDTO dto = new TreeDTO();
				dto.setId(codingSchemesId);
				dto.setName(codingSchemesName);
				dto.setParentId("");
				boolean b = true;
				for (int j = 0; j < newList.size(); j++) {
					TreeDTO ob = (TreeDTO) newList.get(j);
					String id = ob.getId();
					if (codingSchemesId.equals(id)) {
						b = false;
						break;
					}
				}
				if (b) {
					newList.add(dto);
				}
			}

			for (int i = 0; i < returnList.size(); i++) {
				Object[] o = (Object[]) returnList.get(i);
				String codingSchemesId = (String) o[0];
				String versionId = (String) o[2];
				String versionName = (String) o[3];
				TreeDTO dto = new TreeDTO();
				dto.setId(versionId);
				dto.setName(versionName);
				dto.setParentId(codingSchemesId);

				resultList.add(dto);

			}

			for (int k = 0; k < newList.size(); k++) {
				TreeDTO o = (TreeDTO) newList.get(k);

				resultList.add(o);
			}
		}
		dto.setObject(resultList);
		dto.setResultFlag(true);
		dto.setResultDesc("查询成功");
		return dto;
	}
	public boolean findEtVersionsByCodingSchemesId(String codingSchemesId)
			throws Exception {
		boolean bo = false;
		StringBuilder sb = new StringBuilder(
				"select o.etCodingSchemes.codingschemeId from EtVersions o where o.etCodingSchemes.codingschemeId =:codingSchemesId");
		Query query = entityManager2.createQuery(sb.toString());
		query.setParameter("codingSchemesId", codingSchemesId);
		List<String> list = query.getResultList();
		if (ListUtil.isNotEmpty(list)) {
			bo = true;
		}
		return bo;
	}

}
